package com.xszx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xszx.entity.Goods;
import com.xszx.entity.dto.goods.GoodsAddRequest;
import com.xszx.entity.dto.goods.GoodsDeleteRequest;
import com.xszx.entity.dto.goods.GoodsQuerRequest;
import com.xszx.entity.dto.goods.GoodsUpdateRequest;
import com.xszx.service.GoodsService;
import com.xszx.utils.ResultUtils;
import com.xszx.vo.AjaxJson;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 货物相关的控制层
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    // 为何你们都不用@Resource
    @Resource
    private GoodsService goodsService;

    /**
     * 添加货物
     *
     * @param goodsAddRequest 添加货物参数
     * @return 货物id
     */
    @PostMapping("/addGoods")
    public AjaxJson addGoods(@RequestBody GoodsAddRequest goodsAddRequest) {
        if (goodsAddRequest == null){
            throw new RuntimeException("参数错误");
        }
        Goods goods = new Goods();
        // 把表单参数添加到 goods 中
        BeanUtils.copyProperties(goodsAddRequest, goods);

        // 添加记录，会自动回填 gid
        Boolean result = goodsService.addGoods(goods);
        if (!result){
            throw new RuntimeException("添加货物失败");
        }

        return ResultUtils.success(goods.getGid());
    }

    /**
     * 删除货物
     *
     * @param goodsDeleteRequest 删除货物参数
     * @return boolean
     */
    @PostMapping("/deleteGoods")
    public AjaxJson deleteGoods(@RequestBody GoodsDeleteRequest goodsDeleteRequest) {
        if (goodsDeleteRequest == null) {
            throw new RuntimeException("参数错误");
        }

        Boolean result = goodsService.deleteGoodsById(goodsDeleteRequest.getGid());
        return ResultUtils.success(result);
    }

    /**
     * 更新货物
     *
     * @param goodsUpdateRequest 需要更新的参数
     * @return boolean
     */
    @PostMapping("/updateGoods")
    public AjaxJson updateGoods(@RequestBody GoodsUpdateRequest goodsUpdateRequest) {
        if (goodsUpdateRequest == null) {
            throw new RuntimeException("参数错误");
        }
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsUpdateRequest, goods);

        Boolean result = goodsService.updateGoodsById(goods);
        if (!result){
            throw new RuntimeException("操作失败");
        }
        return ResultUtils.success(true);
    }

    /**
     * 分页查询货物信息
     *
     * @param goodsQuerRequest 分页参数(模糊参数包装成了一个类
     * @return 货物信息列表
     */
    @PostMapping("/get/getGoods")
    public AjaxJson getGoodsInfo(@RequestBody GoodsQuerRequest goodsQuerRequest){
        if (goodsQuerRequest == null){
            throw new RuntimeException("参数错误");
        }
        System.out.println(goodsQuerRequest.getRCode());
        // 当前页
        int current = goodsQuerRequest.getCurrent();
        // 当前页的容纳条数
        int pageSize = goodsQuerRequest.getPageSize();

        // 分页查询，拦截 sql，进行分页操作
        PageHelper.startPage(current, pageSize);
        List<Goods> goodsInfoList = goodsService.getGoodsList(goodsQuerRequest);

        if (goodsInfoList == null){
            return ResultUtils.error("参数错误");
        }

        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goodsInfoList);
        return ResultUtils.success(goodsPageInfo);
    }

    @GetMapping("/getGoodsCount")
    public AjaxJson getGoodsCount(){
        AjaxJson j = new AjaxJson();
        //所有货物数量
        int inventoryTotal =  goodsService.getGoodsCount();
        //货物数量趋势增长率
        double inventoryTrend = goodsService.getGoodsGrend();
        j.put("inventoryTotal",inventoryTotal);
        j.put("inventoryTrend",inventoryTrend);
        return j;
    }


    //货物仓库对应的货物详情
    @RequestMapping("/getGoodsInfo/{wid}")
    public AjaxJson getGoodsInfo(@PathVariable("wid") Integer wid){
        System.out.println(wid);
        AjaxJson ajaxJson = new AjaxJson();
        List<Goods> goodsList = goodsService.getGoodsInfo(wid);
        ajaxJson.put("goodsList",goodsList);
        return ajaxJson;
    }
}
